package com.yami.shop.user.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.User;
import com.yami.shop.bean.model.UserExtension;
import com.yami.shop.bean.vo.UserBalanceWithdrawCashConfigVO;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.i18n.I18nMessage;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.BeanUtil;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.security.api.model.YamiUser;
import com.yami.shop.security.api.util.SecurityUtils;
import com.yami.shop.service.SysConfigService;
import com.yami.shop.service.UserExtensionService;
import com.yami.shop.user.common.dto.UserBalanceWithdrawCashDto;
import com.yami.shop.user.common.dto.UserBalanceWithdrawCashSetDto;
import com.yami.shop.user.common.param.RangeTimeParam;
import com.yami.shop.user.common.param.UserBalanceWithdrawCashSaceParam;
import com.yami.shop.user.common.service.UserBalanceWithdrawCashService;
import com.yami.shop.user.common.service.UserInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

/**
 * @author yami
 */
@RestController
@RequestMapping("/p/userBalance/withdrawCash")
@Tag(name = "用户提现接口")
public class UserBalanceWithdrawCashController {
    @Autowired
    private UserBalanceWithdrawCashService userBalanceWithdrawCashService;
    @Autowired
    private SysConfigService sysConfigService;

    @Autowired
    private UserExtensionService userExtensionService;
    @Autowired
    private UserInfoService userInfoService;

    @GetMapping("/info")
    @Operation(summary = "获取提现设置信息")
    public ServerResponseEntity<UserBalanceWithdrawCashSetDto> setInfo() {
        UserBalanceWithdrawCashConfigVO userBalanceWithdrawCashConfigVO = sysConfigService.getSysConfigObject(Constant.BALANCE_WITHDRAW_CASH_CONFIG, UserBalanceWithdrawCashConfigVO.class);
        return ServerResponseEntity.success(BeanUtil.map(userBalanceWithdrawCashConfigVO, UserBalanceWithdrawCashSetDto.class));
    }

    @GetMapping("/page")
    @Operation(summary = "分页查看提现记录")
    public ServerResponseEntity<IPage<UserBalanceWithdrawCashDto>> page(PageParam<UserBalanceWithdrawCashDto> page) {
        return ServerResponseEntity.success(userBalanceWithdrawCashService.userBalanceWithdrawCashDtoPageByUserId(page, SecurityUtils.getUser().getUserId()));
    }

    @PostMapping("/apply")
    @Operation(summary = "用户发起提现申请", description = "填入提现参数")
    public ServerResponseEntity<Map<String, Object>> apply(@Valid @RequestBody UserBalanceWithdrawCashSaceParam userBalanceWithdrawCashSaceParam) {
        YamiUser user = SecurityUtils.getUser();
        if (!user.getEnabled()) {
            // 您已被禁用，不能提现，请联系平台客服
            return ServerResponseEntity.showFailMsg(I18nMessage.getMessage("yami.distribution.user.no.distributor1"));
        }

        // 校验支付密码
        userInfoService.checkPayPassword(SecurityUtils.getUser().getUserId(), userBalanceWithdrawCashSaceParam.getPayPassword());
        // 发起提现
        Map<String, Object> apply = userBalanceWithdrawCashService.apply(userBalanceWithdrawCashSaceParam, BeanUtil.map(user, User.class));
        return ServerResponseEntity.success(apply);
    }

    @GetMapping("/isWithdrawCash")
    @Operation(summary = "当前是否可以提现")
    public ServerResponseEntity<Boolean> isWithdrawCash() {
        // 获取店铺提现设置
        UserBalanceWithdrawCashConfigVO userBalanceWithdrawCashConfigVO = sysConfigService.getSysConfigObject(Constant.BALANCE_WITHDRAW_CASH_CONFIG, UserBalanceWithdrawCashConfigVO.class);
        // 获取店铺设置的提现频率算出时间区间
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - userBalanceWithdrawCashConfigVO.getFrequency());
        // 获取玩家最近的提现次数
        // 判断是否能够提现
        if (userBalanceWithdrawCashService.getCountByRangeTimeAndUserId(new RangeTimeParam(calendar.getTime(), new Date()), SecurityUtils.getUser().getUserId()) >= userBalanceWithdrawCashConfigVO.getNumber()) {
            // 提现次数为x天x次
            String info = I18nMessage.getMessage("yami.distribution.cash.num");
            String num = I18nMessage.getMessage("yami.constant.num");
            String day = I18nMessage.getMessage("yami.day");
            throw new YamiShopBindException(info + userBalanceWithdrawCashConfigVO.getFrequency() + day + userBalanceWithdrawCashConfigVO.getNumber() + num);
        }
        return ServerResponseEntity.success(true);
    }

    @GetMapping("/totalWithdrawCash")
    @Operation(summary = "查看用户总提现金额")
    public ServerResponseEntity<Double> getUserTotalWithdrawCash() {
        YamiUser user = SecurityUtils.getUser();
        if (!user.getEnabled()) {
            // 您已被禁用，不能提现，请联系平台客服
            return ServerResponseEntity.showFailMsg(I18nMessage.getMessage("yami.distribution.user.no.distributor1"));
        }
        // 获取用户的钱包数据
        UserExtension userExtension = userExtensionService.getOne(new LambdaQueryWrapper<UserExtension>().eq(UserExtension::getUserId, user.getUserId()));
        Double totalWithdrawCash = userBalanceWithdrawCashService.getUserTotalWithdrawCash(userExtension.getUserExtensionId());
        return ServerResponseEntity.success(totalWithdrawCash);
    }
}
